import {groupIndex,groupGoods,myGroup,cancleGroup,groupSearch,groupDetail} from './api';
import { Toast } from 'antd-mobile';
import pathToRegexp from 'path-to-regexp';
import { routerRedux } from 'dva/router';
import { Route } from 'dva/router';
import {distributeSearch, distributionIndex} from "../distribution/api";
import {cancleOrderApi} from "../orderList/api";


export default {
    namespace: 'shopOpenGroup',
    state: {
        pageNo:1,
        typeId:"",
        state:"",
        keyword:"",
        groupAdvList:[],
        typeList:[],
        latestOpenGroupList:[],
        myGroupList:[],
        groupSearchList:[],
    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },
        clearState() {
            return {
                groupAdvList:[],
                myGroupList:[],
                latestOpenGroupList:[],
                groupSearchList:[],
                typeList:[],};
        },
    },
    effects: {
        // 拼团首页
        *groupIndexEFF({ }, { put, call, select }) {
            const result = yield call(groupIndex);
            if (result.code == 200) {
                if(result.ret==""){yield put({ type: 'save', data: { groupAdvList:[],typeList:[],latestOpenGroupList:[]} })}else{
                    yield put({ type: 'save', data: {groupAdvList:result.ret.groupAdvList,typeList:result.ret.typeList,latestOpenGroupList:result.ret.latestOpenGroupList} });
                }
            }
            return result
        },
        // 团信息
        *groupDetailEFF({ obj }, { put, call, select }) {
            const result = yield call(groupDetail, { ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret });
            }
            return result
        },
        //拼团首页商品
        *groupGoodsEFF({ obj }, { put, call, select }) {
            const shopOpenGroup = yield select((state)=>state.shopOpenGroup);
            const result = yield call(groupGoods, { typeId:shopOpenGroup.typeId, pageNo:shopOpenGroup.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { groupGoodsList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, groupGoodsList: result.ret.list} });
                    else yield put({ type: 'save', data: { ...result.ret,groupGoodsList: [...shopOpenGroup.groupGoodsList, ...result.ret.list]} });
                }
            }
            return result
        },
        //我的拼团
        *myGroupEFF({ obj }, { put, call, select }) {
            const shopOpenGroup = yield select((state)=>state.shopOpenGroup);
            const result = yield call(myGroup, { state:shopOpenGroup.state, pageNo:shopOpenGroup.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { myGroupList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, myGroupList: result.ret.list} });
                    else yield put({ type: 'save', data: { ...result.ret,myGroupList: [...shopOpenGroup.myGroupList, ...result.ret.list]} });
                }
            }
            return result
        },
        //拼团搜索
        *groupSearchEFF({ obj }, { put, call, select }) {
            const shopOpenGroup = yield select((state)=>state.shopOpenGroup);
            const result = yield call(groupSearch, { keyword:shopOpenGroup.keyword, pageNo:shopOpenGroup.pageNo, ...obj });
            if (result.code == 200) {
                yield put({ type: 'save', data: result.ret  });
                if(result.ret==""){yield put({ type: 'save', data: { groupSearchList: []} })}else{
                    if(result.ret.pageNo <= 1) yield put({ type: 'save', data: {...result.ret, groupSearchList: result.ret.list} });
                    else yield put({ type: 'save', data: { ...result.ret,groupSearchList: [...shopOpenGroup.groupSearchList, ...result.ret.list]} });
                }
            }
            return result
        },
        //取消拼团
        *cancleGroupEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(cancleGroup,obj);
            return result;
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if(pathname.includes('groupSearch')){
                    const match = pathToRegexp('/groupSearch/:data').exec(pathname);
                    dispatch({type:'groupSearchEFF',obj:{keyword:match[1],pageNo:1}})
                }
                if(pathname.includes('groupDetail')){
                    const match = pathToRegexp('/groupDetail/:data').exec(pathname);
                    dispatch({type:'groupDetailEFF',obj:{id:match[1]}})
                }
            });
        },
    },
};